
import Cookies from 'js-cookie'
const TokenKey = 'Admin-Token'
export function getURIParam(key) {
    const reg = new RegExp(`(^|&)${key}=([^&]*)(&|$)`);
    // @ts-ignore
    const paramkey = window.location.search.substr(1).match(reg);
    if (paramkey != null && paramkey[2] != 'false') {
        return decodeURIComponent(paramkey[2]);
    }
    return false;
}

/**
* 是否是生产
* @returns 
*/
export function isProduction() {
    return process.env.NODE_ENV === 'production'
}

export function getMockUserInfo() {
    return {
        Cookie: 'rememberMe=true; username=test; password=fmMNzMvuuliSN1UnxNKSgVAxERpsFS6PnJt0UN6oqq3CEaHLA8Y9RqxF+dk1VdRtO30dAyVsDRuvTOiaRpwocw==; Admin-Token=eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImZmY2I5MGYwLTU2NmQtNDZlMi05MjNhLTBmYzZkYWZjMTNkYiJ9.zIZS4KUBQh7xxVRvbinTXonKuVbS9yf9E2HR4J-ahyKTe5KbK0w5eHfzJM9aGXPDfqYxsuXHSAIRcUUHc-UiXg',
        Authorization: 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImZmY2I5MGYwLTU2NmQtNDZlMi05MjNhLTBmYzZkYWZjMTNkYiJ9.zIZS4KUBQh7xxVRvbinTXonKuVbS9yf9E2HR4J-ahyKTe5KbK0w5eHfzJM9aGXPDfqYxsuXHSAIRcUUHc-UiXg'
    }
}

export function getUserInfo() {
    return {
        Cookie: getToken(),
        Authorization: getToken()
    }
}

/**
* 判断是否是一个字符串
* @param str 
* @returns 
*/
export function isString(str) {
    return typeof str === 'string'
}

/**
* 判断是否是一个boolean值
* @param str 
* @returns 
*/
export function isBoolean(bool) {
    return typeof bool === 'boolean'
}


/**
* 判断是否是一个空值
* @param str 
* @returns 
*/
export function isNull(str) {
    return str === undefined || str === null;
}

/**
* 判断是否是一个Number值
* @param str 
* @returns 
*/
export function isNumber(num) {
    return typeof num === 'number'
}

/**
* 判断是否是一个对象
* @param str 
* @returns 
*/
export function isObject(object) {
    return typeof object === 'object'
}


/**
* 判断是否是一个函数
* @param func 
* @returns 
*/
export function isFunction(func) {
    return typeof func === 'function'
}

export function isArray(arr) {
    return Array.isArray(arr)
}

/**
* 判断字符串是否是number类型
* @param str 
* @returns 
*/
export function isStringNumber(str) {
    const reg = /^(\+|-)?\d+($|\.\d+$)/;
    if (reg.test(str)) {
        return true;
    }

    return false;
}

export function parseValue(value, defaultValue) {
    if (isNull(value)) {
        return defaultValue;
    } else {
        return value;
    }
}

export function cloneArray(object) {
    if (!object) {
        return null;
    }

    if (!isArray(object)) {
        return null;
    }

    return object.concat();
}

export function getToken() {
    return Cookies.get(TokenKey)
}

export function setToken(token) {
    return Cookies.set(TokenKey, token)
}

export function removeToken() {
    return Cookies.remove(TokenKey)
}

